﻿using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.RootInterface.QuerySource;
using EkStudio.Com.UI.Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace EkStudio.com.ui.form
{
    public partial class ModuleQueryUiForm : DevExpress.XtraEditors.XtraForm
    {

        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ModuleQueryUiForm));
        private BillModuleItem bean = null;
        private ProjectRootPanel RootPanel = null;
        private ProjectUnitBillInterface UnitPanel = null;

        private DataTable data = new DataTable("TableName");

        private bool Apply = false;

        public ModuleQueryUiForm(BillModuleItem mod,ProjectRootPanel root,ProjectUnitBillInterface unit)
        {
            InitializeComponent();
            this.bean = mod;
            this.RootPanel = root;
            this.UnitPanel = unit;
            this.InitForm();
        }

        private void InitForm()
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            data.Columns.Add("Caption", typeof(string));
            data.Columns.Add("Source", typeof(string));
            data.Columns.Add("Group", typeof(string));
            data.Columns.Add("languageTexts", typeof(string));
            data.Columns.Add("Sort", typeof(string));
            this.GridPrinterControl.DataSource = data;
            this.GridPrinterView.OptionsBehavior.Editable = !UnitPanel.ReadOnly;
        }

        public void ReLoadList()
        {
            this.ListBoxView.Items.Clear();
            //this.bean.autoInitQueryCode();
            this.data.Rows.Clear();
            this.GridPrinterView.RefreshData();
            this.SqlEdit.Text = "";

            if (this.bean.QuerySource != null && this.bean.QuerySource.Item != null && bean.QuerySource.Item.Count > 0)
            {

                List<BillQuerySchemeItem> list = this.bean.QuerySource.Item;
                ListSchemeItem first = null;
                foreach (BillQuerySchemeItem scheme in list)
                {
                    ListSchemeItem lIt = new ListSchemeItem(scheme.Key, scheme.Caption, scheme.Default_);
                    lIt.Scheme = scheme;
                    if (first == null) first = lIt;
                    this.ListBoxView.Items.Add(lIt);
                }
                //this.LoadMetail(first);
            }
            else
            {
                RootPanel.AddConsoleText("未能找到数据源",false);
            }
            this.BtnSubmit.Enabled = false;
        }

        private void LoadMetail(ListSchemeItem value)
        {
            this.data.Rows.Clear();
            BillQuerySchemeItem scheme = value.Scheme;
            bool table = true;//子表
            if(scheme.Key == this.bean.getKey())
            {
                //主表信息
                table = false;
            }
            //加载字段
            if (scheme.PrinterFields != null && scheme.PrinterFields.Fields != null && scheme.PrinterFields.Fields.Count > 0)
            {
                List<BillQueryPrinterItem> list = scheme.PrinterFields.Fields;
                foreach (BillQueryPrinterItem it in list)
                {
                    DataRow row = this.data.NewRow();
                    row["Caption"] = it.Caption;
                    row["Source"] = it.Key;
                    row["languageTexts"] = it.LanguageTexts;
                    row["Sort"] = it.Sort;
                    BillUiFieldsItem pField = null;
                    foreach (BillUiGroupItems group in this.bean.UiField.FieldsProperties)
                    {
                        if (group.Fields != null && group.Fields.Count > 0)
                        {
                            foreach (BillUiFieldsItem field in group.Fields)
                            {
                                if (field.Key == it.Key && field.Caption == it.Caption)
                                {
                                    pField = field;
                                    break;
                                }
                            }
                        }
                        if (pField != null) break;
                    }
                    if (pField != null)
                    {
                        row["Group"] = pField.ViewBorder.Subject;
                    }
                    else
                    {
                        row["Group"] = "";
                    }

                    this.data.Rows.Add(row);
                }
            }
            //加载数据源
            if (scheme.SqlSource != null)
            {
                AutoDataSource.Checked = scheme.SqlSource.Auto;
                this.SqlEdit.Properties.ReadOnly = scheme.SqlSource.Auto;
                this.SqlEdit.Text = scheme.SqlSource.Content;
            }
            this.GridPrinterView.RefreshData();
            this.BtnSubmit.Enabled = false;
        }

        private void ListBoxView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ListBoxView.SelectedIndex >= 0)
            {
                this.LoadMetail(this.ListBoxView.SelectedItem as ListSchemeItem);
            }
        }


        private class ListSchemeItem
        {
            private string key = "";
            private string caption = "";
            private bool default_ = false;
            private BillQuerySchemeItem scheme = null;


            public ListSchemeItem(string _key, string _caption, bool def)
            {
                this.key = _key;
                this.caption = _caption;
                this.default_ = def;
            }

            public string Key
            {
                get
                {
                    return key;
                }

                set
                {
                    key = value;
                }
            }

            public string Caption
            {
                get
                {
                    return caption;
                }

                set
                {
                    caption = value;
                }
            }

            public bool Default_
            {
                get
                {
                    return default_;
                }

                set
                {
                    default_ = value;
                }
            }

            public BillQuerySchemeItem Scheme
            {
                get
                {
                    return scheme;
                }

                set
                {
                    scheme = value;
                }
            }

            public override string ToString()
            {
                return string.Format("{0}{1}",this.caption,this.default_?"[默认]":"");
            }


        }

        private void BtnMoveup_Click(object sender, EventArgs e)
        {
            if (this.GridPrinterView.FocusedRowHandle > 0)
            {
                int Index = this.GridPrinterView.FocusedRowHandle;
                int PevIndex = Index - 1;
                DataRow ThisRow = data.Rows[Index];
                DataRow PevRow = data.Rows[PevIndex];
                string Caption = (string)PevRow["Caption"];
                string Source = (string)PevRow["Source"];
                string Group = (string)PevRow["Group"];
                string Sort = (string)ThisRow["Sort"];
                string languageTexts = (string)PevRow["languageTexts"];
                //修改上行内容为本行内容
                PevRow["Caption"] = ThisRow["Caption"];
                PevRow["Source"] = ThisRow["Source"];
                PevRow["Group"] = ThisRow["Group"];
                PevRow["Sort"] = ThisRow["Sort"];
                PevRow["languageTexts"] = ThisRow["languageTexts"];
                //修改本行为上行内容
                ThisRow["Caption"] = Caption;
                ThisRow["Source"] = Source;
                ThisRow["Group"] = Group;
                ThisRow["Sort"] = Sort;
                ThisRow["languageTexts"] = languageTexts;
                this.GridPrinterView.RefreshRow(PevIndex);
                this.GridPrinterView.RefreshRow(Index);
                this.Apply = false;
                this.BtnSubmit.Enabled = true;
                this.GridPrinterView.MovePrev();
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (this.GridPrinterView.FocusedRowHandle<(this.data.Rows.Count -1))
            {
                int Index = this.GridPrinterView.FocusedRowHandle;
                int LastIndex = Index + 1;
                DataRow ThisRow = data.Rows[Index];
                DataRow LastRow = data.Rows[LastIndex];
                string Caption = (string)ThisRow["Caption"];
                string Source = (string)ThisRow["Source"];
                string Group = (string)ThisRow["Group"];
                string Sort = (string)ThisRow["Sort"];
                string languageTexts = (string)ThisRow["languageTexts"];
                //修改当行内容为下行内容
                ThisRow["Caption"] = LastRow["Caption"];
                ThisRow["Source"] = LastRow["Source"];
                ThisRow["Group"] = LastRow["Group"];
                ThisRow["Sort"] = LastRow["Sort"];
                ThisRow["languageTexts"] = LastRow["languageTexts"];
                //修改下行内容为本行内容
                LastRow["Caption"] = Caption;
                LastRow["Source"] = Source;
                LastRow["Group"] = Group;
                LastRow["Sort"] = Sort;
                LastRow["languageTexts"] = languageTexts;
                this.GridPrinterView.RefreshRow(LastIndex);
                this.GridPrinterView.RefreshRow(Index);
                this.Apply = false;
                this.BtnSubmit.Enabled = true;
                this.GridPrinterView.MoveNext();
            }
        }

        private void SqlEdit_EditValueChanged(object sender, EventArgs e)
        {
            this.Apply = false;
            this.BtnSubmit.Enabled = true;
        }

        private void BtnSubmit_Click(object sender, EventArgs e)
        {
            if (this.UnitPanel.ReadOnly)
            {
                RootPanel.ShowAlert("提示", "只读模块不可修改");
                return;
            }
            ListSchemeItem it = ListBoxView.SelectedItem as ListSchemeItem;
            if (data.Rows.Count > 0)
            {
                it.Scheme.PrinterFields.Fields.Clear();
                foreach (DataRow row in data.Rows)
                {
                    BillQueryPrinterItem print = new BillQueryPrinterItem();
                    print.Key = (string)row["Source"];
                    print.Caption = (string)row["Caption"];
                    print.LanguageTexts = (string)row["languageTexts"];
                    print.Sort = (string)row["Sort"];
                    it.Scheme.PrinterFields.Fields.Add(print);
                }
            }
            if (!AutoDataSource.Checked)
            {
                it.Scheme.SqlSource.Auto = false;
                it.Scheme.SqlSource.Content = this.SqlEdit.Text;
            }
            else
            {
                it.Scheme.SqlSource.Auto = true;
            }
            
            this.Apply = true;
            this.BtnSubmit.Enabled = false;
            UnitPanel.SaveStatus = false;
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void AutoDataSource_CheckedChanged(object sender, EventArgs e)
        {
            this.SqlEdit.Properties.ReadOnly = AutoDataSource.Checked;
            this.Apply = false;
            this.BtnSubmit.Enabled = true;
        }

        private void GridPrinterView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            this.Apply = false;
            this.BtnSubmit.Enabled = true;
        }
    }
}